package com.bitter.utils;

import java.io.Serializable;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@Component
public class RedisUtils {

	@Autowired
	private RedisTemplate redisTemplate;

	/**
	 * 写入缓存
	 *
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value) {
		boolean result = false;
		try {
			ValueOperations<String, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 写入缓存设置时间
	 *
	 * @param key
	 * @param value
	 * @param expireTime
	 * @return
	 */
	public boolean setEx(final String key, Object value, long expireTime) {
		boolean result = false;
		try {
			ValueOperations<String, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 读取缓存
	 *
	 * @param key
	 * @return
	 */
	public Object get(final String key) {
		Object result = null;
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		result = operations.get(key);
		return result;
	}

	/**
	 * 删除对应的value
	 *
	 * @param key
	 */
	public boolean remove(final String key) {
		if (exists(key)) {
			Boolean delete = redisTemplate.delete(key);
			return delete;
		}
		return false;

	}

	/**
	 * 判断key是否存在
	 *
	 * @param key
	 * @return
	 */
	public boolean exists(final String key) {
		boolean result = false;
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		if (Objects.nonNull(operations.get(key))) {
			result = true;
		}
		return result;
	}
}
